<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="4">
            <el-form-item label="物品名称：" prop="consumeName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入物品名称" v-model="queryParams.consumeName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="规格型号：" prop="ggType" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入规格型号" @keyup.enter="handleQuery"
                v-model="queryParams.ggType" />
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-row justify="end">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">耗材用品列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-button type="success" icon="Plus" @click="batchUse" link
          style="margin-left: 20px; margin-bottom: 5px">批量领用</el-button>
        <el-table ref="mainTableRef" :data="useTable" @selection-change="handleSelChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="物品名称" prop="consumeName" align="center" />
          <el-table-column label="规格型号" prop="ggType" align="center" />
          <el-table-column label="供货商" prop="supplyName" align="center" />
          <el-table-column label="生产厂商" prop="produceName" align="center" />
          <el-table-column label="批号" prop="produceCode" align="center" />
          <el-table-column label="保存位置" prop="saveLocation" align="center" />
          <el-table-column label="入库日期" prop="createDate" align="center" />
          <el-table-column label="在库数量" prop="remainCount" align="center">
            <template #default="scope">
              <span style="color: var(--theme-deep-color)"> {{ scope.row.remainCount }}</span>
              <!-- <el-button link @click="jcxmCountClick(scope.row.consumeId)">{{
                }}</el-button> -->
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="useHcyp(scope.row.consumeId)"
                v-if="scope.row.remainCount">领用</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedHnDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ hnTable.length }} 条数据</p>
      </el-row>
      <el-table :data="hnTable">
        <el-table-column label="检测项目" prop="bucketId" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="notvalueUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 点击领用弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="useOpen" @close="closedDialog" :title="useTitle" width="1600">
      <el-row>
        <p class="pinformation">领用列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="物品名称" prop="consumeName" align="center" />
        <el-table-column label="规格型号" prop="ggType" align="center" />
        <el-table-column label="供货商" prop="supplyName" align="center" />
        <el-table-column label="生产厂商" prop="produceName" align="center" />
        <el-table-column label="批号" prop="produceCode" align="center" />
        <el-table-column label="保存位置" prop="saveLocation" align="center" />
        <el-table-column label="入库人" prop="consumeAdminId" align="center">
          <template #default="scope">
            {{
              proxy.getItemByArray(
                userList,
                "userId",
                scope.row.consumeAdminId,
                "nickName"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column label="入库日期" prop="createDate" align="center" />
        <el-table-column label="余量" prop="createCount" align="center">
          <template #default="scope">
            <span style="color: var(--theme-deep-color)"> {{ scope.row.remainCount }}</span>
            <!-- <el-button link @click="jcxmCountClick(scope.row.consumeId)">{{
                }}</el-button> -->
          </template>
        </el-table-column>
        <el-table-column label="领用数量" prop="outCount" align="center" width="120">
          <template #default="scope">
            <el-input-number v-model="scope.row.outCount" clearable :min="1" style="width: 100%"
              controls-position="right"></el-input-number>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin-top: 20px">
        <el-col :span="4">
          <el-form-item label="领用日期：" prop="saveLocation">
            <el-date-picker v-model="lyForm.outDate" placeholder="请选择领用日期" type="date" value-format="YYYY-MM-DD" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="领用人：" prop="createUserId" label-width="80">
            <el-select v-model="lyForm.createUserId" filterable clearable>
              <el-option v-for="item in userList" :value="item.userId" :key="item.userId"
                :label="item.nickName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4" style="padding-left: 20px">
          <el-button type="success">领用人签字</el-button>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用途：" prop="remark">
            <el-input maxlength="100" clearable v-model="lyForm.remark"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-row justify="end">
            <el-button type="success" @click="saveLy">确 定</el-button>
            <el-button type="danger" @click="useOpen = false">取 消</el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzOutin">
import {
  newListLedger,
  hcypViewForm,
  batchHcypOut,
} from "@/api/resource/hcyp";
import { getCurrentTime, getCurrentUser } from "@/api/common";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";
const { proxy } = getCurrentInstance();
const useTable = ref([]);
const multiplexTable = ref([]);
const hnTable = ref([]);
const jcxmList = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const useOpen = ref(false);
const useTitle = ref("耗材用品领用");
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
function getList() {
  newListLedger(queryParams.value).then((res) => {
    useTable.value = res.rows;
    total.value = res.total;
  });
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function closedDialog() {
  multiplexTable.value = [];
  count.value = 0;
  lyForm.value = {};
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

const lyForm = ref({});

function batchUse() {
  getCurrentTime().then((res) => {
    lyForm.value.outDate = res.data;
  });
  getCurrentUser().then((res) => {
    lyForm.value.createUserId = res.data;
  });
  useOpen.value = true;
}

function useHcyp(id) {
  hcypViewForm(id).then((res) => {
    multiplexTable.value = [res.data];
    lyForm.value.remark = res.data.remark;
    lyForm.value.outDate = res.data.outDate;
    if (!lyForm.value.createUserId) {
      getCurrentUser().then((res) => {
        lyForm.value.createUserId = res.data;
      });
    }
    if (!lyForm.value.outDate) {
      getCurrentTime().then((res) => {
        lyForm.value.outDate = res.data;
      });
    }
    useOpen.value = true;
  });
}

const isGreater = ref(true);
function saveLy() {
  lyForm.value.consumeOutList = multiplexTable.value.map((item, index) => {
    if (isGreater.value) {
      if (item.outCount > item.remainCount) {
        isGreater.value = false;
      }
    }
    let temp = {};
    if (item.outCount === "0" || item.outCount === 0 || !item.outCount) {
      return { consumeId: item.consumeId };
    } else {
      return { consumeId: item.consumeId, outCount: item.outCount };
    }
  });
  if (isGreater.value) {
    batchHcypOut(lyForm.value).then((res) => {
      proxy.$modal.msgSuccess("领用成功");
      useOpen.value = false;
      getList();
    });
  } else {
    proxy.$modal.msgError("存在领用量大于剩余量的输入");
    isGreater.value = true;
  }
}

const count = ref(0);
function outGgChange() {
  if (lyForm.value.outGg) {
    if (count.value === 0) {
      count.value = lyForm.value.remainCount;
    }
    if (lyForm.value.outGg > count.value) {
      lyForm.value.outGg = count.value;
      lyForm.value.remainCount = 0;
    } else {
      lyForm.value.remainCount = count.value - lyForm.value.outGg;
    }
  } else {
    if (count.value !== 0) {
      lyForm.value.remainCount = count.value;
    }
  }
}

function outTypeChange() {
  if (!lyForm.value.outType) {
    lyForm.value.outGg = "";
    outGgChange();
  }
}

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

function closedHnDialog() {
  hnTable.value = [];
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
